package com.woniuxy.reader.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.reader.entity.*;
import com.woniuxy.reader.mapper.*;
import com.woniuxy.reader.service.MainService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 主页业务层实现类
 * @author zh_o
 * @date 2020-10-15
 */
@Service
public class MainServiceImpl implements MainService {

    @Resource
    private CollectionMapper collectionMapper;

    @Resource
    private BookMapper bookMapper;

    @Resource
    private AuthorMapper authorMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private BookDataMapper bookDataMapper;

    @Resource
    private ChapterMapper chapterMapper;

    /**
     * 初始化主页
     * @return
     */
    @Override
    public Map<String, Object> init() {
        // 定义Map，封装响应数据
        HashMap<String, Object> initMap = new HashMap<>(16);
        // 封装本周强推
        initMap.put("weekList", getWeekList());
        // 封装 TOP
        initMap.put("topList", getTopList());
        // 封装今日必读
        initMap.put("todayList", getTodayList());
        // 封装新鲜上架
        initMap.put("newList", getNewList(5));
        // 封装新书精选
        initMap.put("newBookList", getNewList(10));
        // 封装完结好书
        initMap.put("finishList", getFinishList());
        // 封装分类
        initMap.put("categoryList", getCategoryList());
        // 封装底部排行榜
        ArrayList<Ranking> rankingList = new ArrayList<>();
        String[] rankingNames = {"原创畅销榜", "原创上架榜", "原创风云榜", "原创人气榜"};
        for (int i = 0; i < rankingNames.length; i++) {
            Ranking ranking = new Ranking();
            ranking.setId(i + 1);
            ranking.setName(rankingNames[i]);
            ranking.setList(getRankingList());
            rankingList.add(ranking);
        }
        initMap.put("rankingList", rankingList);
        // 封装最近更新
        initMap.put("chapterList", getChapterList());
        return initMap;
    }

    /**
     * 本周强推，本周收藏量前 5 书籍
     */
    @Override
    public List<Book> getWeekList() {
        // 从收藏表获取本周收藏量前五书籍id
        List<Integer> idList = collectionMapper.getWeekList();
        // 根据 id 获取书籍信息
        List<Book> bookList = bookMapper.selectBatchIds(idList);
        // 获取书籍对应作者与分类信息
        getBookInfo(bookList);
        return bookList;
    }

    /**
     * 点击量前三书籍
     */
    @Override
    public List<Book> getTopList() {
        // 获取点击量前三书籍id
        Page<BookData> pages = bookDataMapper.selectPage(new Page<>(0, 3),
                new QueryWrapper<BookData>().select(BookData.BOOK_ID).orderByDesc(BookData.CLICK_NUMBER));
        // 取出书籍 id
        ArrayList<Integer> idList = new ArrayList<>();
        for (BookData record : pages.getRecords()) {
            idList.add(record.getBookId());
        }
        // 根据id获取书籍信息
        List<Book> bookList = bookMapper.selectBatchIds(idList);
        // 获取书籍对应作者与分类信息
        getBookInfo(bookList);
        return bookList;
    }

    /**
     * 今日必读，随机获取三本精品书籍
     */
    @Override
    public List<Book> getTodayList() {
        // 查询所有精品书籍
        List<Book> bookList = bookMapper.selectList(new QueryWrapper<Book>().eq(Book.FLAG, 2));
        // 随机获取三本书籍
        Set<Book> bookSet = RandomUtil.randomEleSet(bookList, 2);
        bookList.clear();
        bookList.addAll(bookSet);
        // 获取书籍对应作者与分类信息
        getBookInfo(bookList);
        return bookList;
    }

    /**
     * 新鲜上架，精选新书5本
     */
    @Override
    public List<Book> getNewList(Integer number) {
        // 查询精品书籍根据创建时间排序
        Page<Book> bookPage = bookMapper.selectPage(new Page<Book>(0, number), new QueryWrapper<Book>().eq(Book.FLAG, 2).orderByDesc(Book.CREATE_TIME));
        // 获取书籍对应作者与分类信息
        getBookInfo(bookPage.getRecords());
        return bookPage.getRecords();
    }

    /**
     * 完结好文，最近完结书十本
     */
    @Override
    public List<Book> getFinishList() {
        // 查询完结书籍根据完结时间排序
        Page<Book> bookPage = bookMapper.selectPage(new Page<>(0, 10), new QueryWrapper<Book>().eq(Book.STATUS, 2).orderByDesc(Book.UPDATE_TIME));
        // 获取书籍对应作者与分类信息
        getBookInfo(bookPage.getRecords());
        return bookPage.getRecords();
    }

    /**
     * 获取分类综合列表
     */
    @Override
    public List<Category> getCategoryList() {
        // 定义 list
        ArrayList<Category> categoryList = new ArrayList<>();
        // 获取每个分类
        for (int i = 1; i <= 4; i++) {
            categoryList.add(getCategory(i));
        }
        return categoryList;
    }

    /**
     * 获取底部排行榜
     */
    @Override
    public List<Book> getRankingList() {
        // 获取书籍信息
        List<Book> books= bookMapper.selectList(new QueryWrapper<Book>().ne(Book.FLAG, 0));
        // 随机取 10 本
        ArrayList<Book> bookList = new ArrayList<>(RandomUtil.randomEleSet(books, 10));
        // 获取书籍对应信息
        getBookInfo(bookList);
        return bookList;
    }

    /**
     * 获取最近更新章节 30
     */
    @Override
    public List<Chapter> getChapterList() {
        // 获取30条章节数据根据更新时间排序
        List<Chapter> chapterList = chapterMapper.selectPage(new Page<>(0, 30), new QueryWrapper<Chapter>().orderByDesc(Chapter.UPDATE_TIME)).getRecords();
        // 获取章节对应书籍、分类与作者信息
        for (Chapter chapter : chapterList) {
            // 获取书籍信息
            Book book = bookMapper.selectById(chapter.getBookId());
            chapter.setBookName(book.getBookName());
            chapter.setDescription(book.getDescription());
            // 获取作者信息
            Author author = authorMapper.selectById(book.getAuthorId());
            chapter.setAuthorName(author.getPenName());
            // 获取分类信息
            Category category = categoryMapper.selectById(book.getCategoryId());
            chapter.setCategoryName(category.getCategoryName());
        }
        return chapterList;
    }

    /**
     * 根据分类id随机获取其中8本书籍
     * @param id
     * @return
     */
    private Category getCategory(Integer id) {
        // 获取分类信息
        Category category = categoryMapper.selectById(id);
        category.setCategoryName(category.getCategoryName() + "综合");
        // 获取书籍信息
        ArrayList<Book> bookList = new ArrayList<>(RandomUtil.randomEleSet(bookMapper.selectList(new QueryWrapper<Book>().eq(Book.CATEGORY_ID, id).ne(Book.FLAG, 0)), 8));
        // 获取书籍对应信息
        getBookInfo(bookList);
        // 封装数据到分类实体
        category.setBookList(bookList);
        return category;
    }

    /**
     * 获取书籍对应作者与分类信息
     */
    private void getBookInfo(List<Book> bookList) {
        // 获取书籍对应作者与分类信息
        for (Book book : bookList) {
            // 获取分类名称
            book.setCategoryName(categoryMapper.selectById(book.getCategoryId()).getCategoryName());
            // 获取作者笔名
            book.setAuthorName(authorMapper.selectById(book.getAuthorId()).getPenName());
        }
    }

}
